package jyc_util.a_review.callable;

import java.util.concurrent.*;

/**
 * @author haomin
 * @date 2022/09/23 18:35
 **/
public class Main {
    // 计算fib数的Callable的子类（封装了"返回值"的线程）
    static class FibCalc implements Callable<Long>{
        private final int n;
        FibCalc(int n){
            this.n = n;
        }
        private long fib (int n){
            if(n == 0 || n == 1) return 1;
            return fib(n-1) + fib(n-2);
        }
        @Override
        public Long call() throws Exception {
            return fib(n);
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService service = Executors.newSingleThreadExecutor();
        FibCalc task = new FibCalc(40);

        Future<Long> future = service.submit(task);

        // 这一步实际上是在等任务计算完成，所以，时间可能需要很久
        Long r = future.get();

        System.out.println(r);

    }
}